/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package contact;

import java.sql.Statement;
import java.util.Date;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Heri Setiyawan
 */
public class ContactControl {
koneksis hub = new koneksis();
public String contact_name,contact_number,contact_mail,datecreated,datemake;
public String dateTimeNow;
    public ContactControl() {
        hub.makeKoneksi();
    }

    public String getDateTimeNow() {
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = new Date();
        
        return dateFormat.format(date);
    }

    public void setContact_name(String concat_name) {
        this.contact_name = concat_name;
    }

    public void setContact_number(String contact_number) {
        this.contact_number = contact_number;
    }

    public void setContact_mail(String contact_mail) {
        this.contact_mail = contact_mail;
    }

    public void setDatecreated(String datecreated) {
        this.datecreated = datecreated;
    }

    public void setDatemake(String datemake) {
        this.datemake = datemake;
    }

    public String getContact_name() {
        return contact_name;
    }

    public String getContact_number() {
        return contact_number;
    }

    public String getContact_mail() {
        return contact_mail;
    }

    public String getDatecreated() {
        return datecreated;
    }

    public String getDatemake() {
        return datemake;
    }
    
    public void isNotif(String notif){
        JOptionPane.showMessageDialog(null, notif,"Message",JOptionPane.OK_OPTION);
    }
    
    public void ContactInsert(){
        String sql = "CALL contact_insert('"+contact_name+"','"+contact_number+"','"+contact_mail+"','"+this.getDateTimeNow()+"')";
        try{
            Statement st = hub.conn.createStatement();
            st.executeQuery(sql);
            this.isNotif("inserting success");
        }catch(Exception q){
            this.isNotif("Error insert \n"+ q);
        }
    }
    
    public void ContactUpdate(){
        
    }
    
    public void ContactDeleted(){
        
    }
    
    public void ContactFind(){
        
    }
    
    public void ContactSelectOne(){
        
    }
    
    public DefaultTableModel ContactDataView(){
        String Title[] = {"ID","NAME","NUMBER","MAIL"};
        DefaultTableModel dtm = new DefaultTableModel(null,Title);
        String sql = "CALL contact_getAll()";
        String data[] = new String[5];
        try{
            hub.rs = hub.st.executeQuery(sql);
            while(hub.rs.next()){
                data[0]=hub.rs.getString(1);
                data[1]=hub.rs.getString(2);
                data[2]=hub.rs.getString(3);
                data[3]=hub.rs.getString(4);
              //  data[4]=hub.rs.getString(5);
              //  data[5]=hub.rs.getString(6);
                dtm.addRow(data);
            }
            
        }catch(Exception q){
            this.isNotif("Error insert \n"+ q);
        }
        return dtm;
    }
}
